package com.situ.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TimeServlet extends HttpServlet{
	
	public TimeServlet() {
		System.out.println("TimeServlet.TimeServlet()");
	}
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		String maxOnLine = config.getInitParameter("maxOnLine");
		System.out.println("maxOnLine:" + maxOnLine);
	}
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ServletConfig servletConfig = getServletConfig();
		String maxOnLine = servletConfig.getInitParameter("maxOnLine");
		System.out.println("maxOnLine:" + maxOnLine);
		
		ServletContext servletContext = getServletContext();
		String pageSize = servletContext.getInitParameter("pageSize");
		System.out.println("pageSize:" + pageSize);
		
		// 告诉浏览器返回的是一个网页
		resp.setContentType("text/html");
		
		Date date = new Date();
		System.out.println(date);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String nowTime = simpleDateFormat.format(date);
		
		// 返回给浏览器
		PrintWriter printWriter = resp.getWriter();
		printWriter.println("<p>" +nowTime + "</p>");
		printWriter.close();
	}
	
//	public static void main55(String[] args) {
//		int result = factorial(5);
//		System.out.println(result);
//	}
//
//	private static int factorial(int n) {
//		int result = 1;
//		for (int i = n; i >= 1; i--) {
//			result = result * i;
//		}
//		return result;
//	}
//
//	public static void main(String[] args) {
//		int result = f(5);
//		System.out.println(result);
//	}
//
//	private static int f(int n) {
//		if(n == 1) {
//			return 1;
//		}
//	    return n * f(n-1);
//	}

}
